//
//  BENoramlDottedLine.m
//  s.
//
//  Created by fanshijian on 2022/9/1.
//  Copyright © 2022 J. All rights reserved.
//

#import "BENoramlDottedLine.h"

@implementation BENoramlDottedLine

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.isVertical = YES;
        self.lineDashPattern = @[@4, @2];
        self.lineWidth = 1.f;
        self.strokeColor = KLColor(@"#D8D8D8");
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self addDottedLineLayer];
}

- (void)addDottedLineLayer {
    UIBezierPath *path = [UIBezierPath bezierPath];
    if (self.isVertical) {
        CGFloat x = self.bounds.size.width*0.5;
        CGFloat s_y = 0;
        [path moveToPoint:CGPointMake(x, s_y)];
        [path addLineToPoint:CGPointMake(x, self.bounds.size.height)];
    }else {
        CGFloat x = 0;
        CGFloat s_y = self.bounds.size.height*0.5;
        [path moveToPoint:CGPointMake(x, s_y)];
        [path addLineToPoint:CGPointMake(self.bounds.size.width, s_y)];
    }
    
    if (self.dottodLayer == nil) {
        CAShapeLayer *border = [CAShapeLayer layer];
        //虚线的颜色
        if (self.strokeColor) border.strokeColor = self.strokeColor.CGColor;//KLColor(@"#D8D8D8").CGColor;
        //填充的颜色
        border.fillColor = [UIColor clearColor].CGColor;
        //设置路径
        border.path = path.CGPath;
        border.frame = self.bounds;
        //虚线的宽度
        border.lineWidth = self.lineWidth;//1.f;
        //设置线条的样式
        //    border.lineCap = @"square";
        //虚线的间隔
        border.lineDashPattern = self.lineDashPattern;//@[@4, @2];
        self.layer.cornerRadius = 5.f;
        self.layer.masksToBounds = YES;
        [self.layer addSublayer:border];
        self.dottodLayer = border;
    }else {
        self.dottodLayer.path = path.CGPath;
    }
}
@end
